import { defineStore } from 'pinia';
import { getMenuNav, getMenuPermissions, getUserInfo, getDictTypes, getAgentList } from '@/api';
import { mergeServerRoute, getSysRouteMap } from '@/utils/routeUtils';
import { useUserStore } from '@/stores';

// 应用数据管理
export const useAppStore = defineStore('app', () => {

    // 菜单数据
    const menus = ref<any[]>([]);

    // 权限数据
    const permissions = ref<any[]>([]);

    // 字典数据
    const dicts = ref<any[]>([]);

    // 智能体数据
    const agents = ref<any[]>([{
        "id": "1921838545713623041",
        "name": "总结类智能体",
        "addr": "http://192.168.1.151:8109/v1/chat-messages",
        "keyStr": "app-E6JC7coIqP6ik4MT0rzQqiuq",
        "createDate": "2025-05-12 16:03:10"
    }]);

    // 合并后的路由数据
    const mergedRoutes = ref<any[]>([]);

    // 深度思考开关状态
    const isDeepThinking = ref<boolean>(false);

    // 设置菜单
    const setMenus = (menuData: any[]) => {
        menus.value = menuData;
    };

    // 设置权限
    const setPermissions = (permissionData: any[]) => {
        permissions.value = permissionData;
    };

    // 设置字典
    const setDicts = (dictData: any[]) => {
        dicts.value = dictData;
    };

    // 设置智能体
    const setAgents = (agentData: any[]) => {
        agents.value = agentData;
    };

    // 设置合并路由
    const setMergedRoutes = (routeData: any[]) => {
        mergedRoutes.value = routeData;
    };

    // 设置深度思考状态
    const setDeepThinking = (value: boolean) => {
        isDeepThinking.value = value;
    };

    // 初始化应用数据
    const initApp = async () => {
        try {
            // 如果没有用户信息，则获取用户信息
            const apiCalls = [
                getMenuNav(), // 加载菜单
                getMenuPermissions(), // 加载权限
                getDictTypes(), // 加载字典
                getAgentList(), // 加载智能体
                getUserInfo() // 加载用户信息
            ];
            const results = await Promise.all(apiCalls);

            let [menusRes, permissionsRes, dictsRes, agentsRes, userRes] = results;

            // 更新 store 数据
            setPermissions(Array.isArray(permissionsRes.data) ? permissionsRes.data : []);
            setDicts(Array.isArray(dictsRes.data) ? dictsRes.data : []);
            const userStore = useUserStore();
            userStore.setUserInfo(userRes.data);
            // 处理智能体数据，添加用户信息
            const processedAgents = agentsRes.data?.list || [];

            setAgents(processedAgents);

            // 合并服务端菜单和系统路由
            const menuData = Array.isArray(menusRes.data) ? menusRes.data : [];
            const routes = mergeServerRoute(menuData, getSysRouteMap());
            setMergedRoutes(routes);
            setMenus(menuData);

            console.log('应用数据初始化成功');
            return true;
        } catch (error) {
            console.error('初始化应用数据错误', error);
            return false;
        }
    };

    // 清空应用数据
    const clearAppData = () => {
        menus.value = [];
        permissions.value = [];
        dicts.value = [];
        agents.value = [];
        mergedRoutes.value = [];
        isDeepThinking.value = false;
    };

    return {
        menus,
        permissions,
        dicts,
        agents,
        mergedRoutes,
        isDeepThinking,
        setMenus,
        setPermissions,
        setDicts,
        setAgents,
        setMergedRoutes,
        setDeepThinking,
        initApp,
        clearAppData,
    };
}, {
    persist: true,
});